package com.itcast.demo.remoterfc.httpclient;

import com.alibaba.fastjson.JSON;
import com.itcast.demo.remoterfc.httpurlconnection.ProductBean;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriBuilder;

import java.util.HashMap;

@RestController
@RequestMapping("/httpclient")
public class HttpClientController {
    @PostMapping("doget")
    public void doget() throws Exception {
        String baseUrl = "";
        String basicAppToken = "";

        URIBuilder builder = new URIBuilder(baseUrl);
        builder.setParameter("pageSize", "5");
        builder.setParameter("pageIndex", "1");

        ProductBean productBean = new ProductBean();
        productBean.setOrder_id(new String[]{"134343434"});
        String strs = JSON.toJSONString(productBean);
        builder.setParameter("params", strs);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(builder.build());
        httpGet.setHeader("Authorization", basicAppToken);

        CloseableHttpResponse response = httpClient.execute(httpGet);
        try {
            if (response.getStatusLine().getStatusCode() == 200) {
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println("响应内容：" + responseBody);
            } else {
                System.out.println("请求失败： 状态码：" + response.getStatusLine().getStatusCode());
            }
        } finally {
            response.close();
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @PostMapping("dopost")
    public void dopost() throws Exception {
        String baseUrl = "";
        String basicAppToken = "";

        URIBuilder builder = new URIBuilder(baseUrl);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(builder.build());
        httpPost.setHeader("Authorization", basicAppToken);
        httpPost.setHeader("Content-Type", "application/json");

        HashMap<String, String> map = new HashMap<>();
        map.put("network_id", "N10667");
        String strs = JSON.toJSONString(map);

        StringEntity entity = new StringEntity(strs, "UTF-8");
        httpPost.setEntity(entity);

        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            if (response.getStatusLine().getStatusCode() == 200) {
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println("响应内容：" + responseBody);
            } else {
                System.out.println("请求失败： 状态码：" + response.getStatusLine().getStatusCode());
            }
        } finally {
            response.close();
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
